﻿using System;
using System.Web.Mvc;
using WROA.ActionFilter;
using WROA.Helper;
using WROA.Models.Entitys;
using WROA.Models.IServices;
using WROA.Models.Services;
using WROA.Models.ViewModels.AccountModels;

namespace WROA.Controllers
{

    [HandleError]
    public class AccountController : Controller
    {

        //登录界面
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Login()
        {
            return View();
        }

        // 判断是否能成功登录
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Login(FormCollection form)
        {
            string validateCode = (Session["ValidateCode"] ?? "").ToString();
            if (validateCode != form["code"].ToString())
            {
                ModelState.AddModelError("Code", "验证码错误!");
                return View();
            }
            IUserService user = new UserService();
            string userName = (form["UserId"] ?? "").Trim();
            string userPassword = MD5Safe.EncryptPassword((form["UserPwd"] ?? "").Trim());//密码加密后进行验证
            //验证失败时
            if (!user.IsPass(userName, userPassword))
            {
                ModelState.AddModelError("Error", "密码或用户名错误！");
                return View();
            }
            string roleName = "管理员";//数据库返回
            AddCookit addCookit = new AddCookit();
            addCookit.AddCookitToLoginUser(userName, roleName);//添加角色的用户名和角色添加Cookit到客户端
            return RedirectToAction("Index", "Home");
        }

        //// 将生成的验证码存入Session，并输出验证码图片
        public ActionResult AuthCode()
        {
            ValidateCode vCode = new ValidateCode();
            string code = vCode.CreateValidateCode(5);
            Session["ValidateCode"] = code;
            byte[] bytes = vCode.CreateValidateGraphic(code);
            return File(bytes, @"image/jpeg");
        }

        //注销(清空session,返回登录界面)
        [RoleAuthorize(RoleName = new string[] { "管理员" })]
        public ActionResult LogOut()
        {
            Session.Abandon();
            return RedirectToAction("Login");
        }

        //登录测试页
        public ActionResult LoginTest()
        {
            return View();
        }

        /// <summary>
        /// 注册页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Regist()
        {
            RegistView model = new RegistView();
            return View(model);
        }

        /// <summary>
        /// 注册页面Post
        /// </summary>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Regist(Users UserCreat)
        {
            if (ModelState.IsValid)
            {
                IUserService us = new UserService();
                Users user = new Users();
                user = UserCreat;
                user.U_registdate = DateTime.Now;
                user.Role = us.GetRole(UserCreat.Role.Ro_name);
                user.Position = us.GetPosition(UserCreat.Position.Po_name);
                //user.U_name = form["UserName"];
                //user.U_password = form["UserPassword"];
                //user.U_realname = form["UserRealName"];
                //user.U_gender = form["UserGender"];
                //user.U_email = form["UserEmail"];
                //user.U_phone = form["UserPhone"];
                //user.U_mobile = form["UserMobile"];
                //user.U_registdate = DateTime.Now;
                //user.role = us.GetRole(form["UserRole"]);
                //user.position = us.GetPosition(form["UserPosition"]);
                us.Save(user);
                return View();
            }
            RegistView model = new RegistView();
            model.UserData = UserCreat;
            return View(model);
        }
    }
}
